package org.qu.userdao.action;


import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;
import org.omg.CORBA.Request;
import org.qu.factory.DaoFactory;
import org.qu.util.MailUtil;
import org.qu.util.dao.ConnectionData;
import org.qu.vo.User;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UserManager extends ActionSupport {
	private User user;
	private boolean flag=false;

	/**
	 * 登陆验证用户
	 * @return
	 * @throws Exception
	 */
	public String isLogin() throws Exception{
		if(user!=null){
			flag=DaoFactory.factoryUserDao(new ConnectionData().getConnection()).isLogin(user);
			
			if(flag){
				ActionContext.getContext().getSession().put("user", user);
			}
		}
		JSONObject json=new JSONObject();
		
		json.put("flag",flag);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		
		response.setCharacterEncoding("gbk");
		
		response.setContentType("text/html");
		
		PrintWriter out = response.getWriter();
		
		out.print(json);
		
		out.close();	
		
		return Action.SUCCESS;
	}
	
	/**
	 * 注册ajax验证用户名是否存在
	 * @return
	 */
	public String isName() throws Exception{
		
		if(user!=null){
			
			flag=DaoFactory.factoryUserDao(new ConnectionData().getConnection()).isName(user.getEmail());
			
		}
		JSONObject json=new JSONObject();
		
		json.put("flag",flag);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		
		response.setCharacterEncoding("gbk");
		
		response.setContentType("text/html");
		
		PrintWriter out = response.getWriter();
		
		out.print(json);
		
		out.close();		

		return Action.SUCCESS;
	}
	
	/**
	 * 注册成功后转到欢迎页面
	 * @return
	 */
	public String add(){
		if(user!=null){
			ServletActionContext.getRequest().setAttribute("email",user.getEmail());  //创建成功，前端页面根据此值显示引导程序
			
			/**************加载通过邮件确认系统完成注册******************/
			MailUtil mail=new MailUtil();
			
			mail.send("hanjin7278@126.com", "信息核实,请点击&nbsp;&nbsp;" +
					"<a href='http://127.0.0.1"+ServletActionContext.getRequest().getContextPath()+"/UserManager/user!isEmailReturn.action?user.email="+user.getEmail()+"&user.password="+user.getPassword()+"' target='_blank'>核实</a>&nbsp;<br/>如果没有跳转请复制地址到浏览器中 http://127.0.0.1"+ServletActionContext.getRequest().getContextPath()+"/UserManager/user!isEmailReturn.action?user.email="+user.getEmail()+"&user.password="+user.getPassword()
					,user.getEmail());
		}
		return "welcome";
	}
	
	
	/**
	 * 邮件确认OK返回个人主页面
	 * @return
	 */
	public String isEmailReturn(){
		/**从这里验证user用户，如果当前注册的用户，如果从邮箱返回则保存到数据库中**/
		boolean flag=false;
		
		if(null != this.user){
			
			User tempUser=DaoFactory.factoryUserDao(new ConnectionData().getConnection()).findById(this.user);
			
			if(tempUser==null){
				
				flag=DaoFactory.factoryUserDao(new ConnectionData().getConnection()).doCreate(user);
				
			}
		}
		return "setpersoninfo";
	}
	
	/**
	 * 个人中心
	 * @return
	 */
	public String userCenter(){
		User user=(User) ActionContext.getContext().getSession().get("user");
		if(null != user){
			
		}
		return "center";
	}
	
	

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}
}
